package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.IotTimedTasks;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.IotTimedTasksDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2020-10-05 16:33:37
 */
public class IotTimedTasksConverts {

    private static Logger LOG = LoggerFactory.getLogger(IotTimedTasksConverts.class);

    public static List<IotTimedTasksDTO> convert(List<IotTimedTasks> iotTimedTasksList) {
        if (CollectionUtils.isEmpty(iotTimedTasksList)) {
            return Collections.EMPTY_LIST;
        }

        List<IotTimedTasksDTO> iotTimedTasksListResult = Lists.newArrayList();
        for (IotTimedTasks iotTimedTasks : iotTimedTasksList) {
            iotTimedTasksListResult.add(convert(iotTimedTasks));
        }
        return iotTimedTasksListResult;
    }

    public static IotTimedTasksDTO convert(IotTimedTasks iotTimedTasks) {
        try {
            return BeanUtils.copy(iotTimedTasks, IotTimedTasksDTO.class);
        } catch (Exception e) {
            LOG.error("IotTimedTasks到IotTimedTasksDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "IotTimedTasks到IotTimedTasksDTO转换失败", e);
        }
    }

    public static IotTimedTasks convert(IotTimedTasksDTO iotTimedTasksDTO) {
        try {
            return BeanUtils.copy(iotTimedTasksDTO, IotTimedTasks.class);
        } catch (Exception e) {
            LOG.error("IotTimedTasksDTO到IotTimedTasks转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "IotTimedTasksDTO到IotTimedTasks转换失败", e);
        }
    }
}